home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 May: Tool Chest / Developer CD Series May 1996 (Tool Chest) (Apple Computer) (1996).iso / Sample Code / Http Server / •OT_Classes / TNetworkStream.cp < prev    next >
Encoding:
Text File  |  1996-01-11  |  6.2 KB  |  251 lines  |  [TEXT/CWIE]

  1. //    TNetworkStream.cp - Macintosh OpenTransport Network Stream IO class object
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinne Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. #include "TNetworkStream.h"
  18. #include <iomanip.h>
  19.  
  20. #define DEFAULT_CHUNKSIZE 512
  21.  
  22.  
  23. // ---------------------------------------------------------------------------
  24. // --------------------------- Network Output Stream -------------------------
  25. // ---------------------------------------------------------------------------
  26.  
  27. // ---------------------------------------------------------------------------
  28. //     ~TNetworkIOStream()
  29. // ---------------------------------------------------------------------------
  30. //    Destructor
  31. TNetworkIOStream::~TNetworkIOStream()
  32. {
  33. }
  34.  
  35.  
  36. // ---------------------------------------------------------------------------
  37. // --------------------------- Network Stream Buffer -------------------------
  38. // ---------------------------------------------------------------------------
  39.  
  40. // ---------------------------------------------------------------------------
  41. //     ~TNetworkBuf
  42. // ---------------------------------------------------------------------------
  43. //    Destructor
  44.  
  45. TNetworkBuf::~TNetworkBuf()
  46. {
  47.     close();
  48. };
  49.  
  50.  
  51. // ---------------------------------------------------------------------------
  52. //     attach(ep)
  53. // ---------------------------------------------------------------------------
  54. //    attach Network stream to endpoint
  55.  
  56. void TNetworkBuf::attach(EndpointRef ep, TEndpointInfo* info)
  57. {
  58.  
  59. // insure that it isn't open already
  60.     if (is_open()) return;
  61.  
  62. // OK to set up
  63.     fEndPoint = ep;
  64.     fInfo =     info;
  65.     
  66. // calculate optimal chunk size
  67.     fChunkSize = (fInfo->tsdu < 1) ? DEFAULT_CHUNKSIZE :fInfo->tsdu;
  68. }
  69.  
  70.  
  71. // ---------------------------------------------------------------------------
  72. //     close()
  73. // ---------------------------------------------------------------------------
  74. //    close network stream
  75.  
  76. void  TNetworkBuf::close()
  77. {
  78.     if (is_open())  {
  79. // blow ya load
  80.         if(fBuffer)    sync();
  81.  
  82. // clean up after
  83.         fEndPoint = kOTInvalidRef;
  84.         fBuffer = nil;
  85.         setp(0, 0);
  86.  
  87. // initiate teardown here???
  88.     }
  89. }
  90.  
  91.  
  92. // ---------------------------------------------------------------------------
  93. //     sync()
  94. // ---------------------------------------------------------------------------
  95. //    sync buffer
  96.  
  97. int TNetworkBuf::sync()
  98. {
  99. // can only output when open.
  100.     if (! is_open()) return EOF;
  101.  
  102. // flush buffers
  103.     flush_output();
  104.     
  105.     return 0;
  106. }
  107.  
  108.  
  109. // ---------------------------------------------------------------------------
  110. //     overflow(c)
  111. // ---------------------------------------------------------------------------
  112. //    process chars in bufffer area 
  113.  
  114. int TNetworkBuf::overflow(int c)
  115. {
  116. // allocate buffer space if needed
  117.     if(pbase() == 0 && !doallocate())  return EOF;
  118.     
  119. // if flush requested force output
  120.     if(c == EOF) return flush_output();
  121.  
  122. // check if we are at end of buffer
  123.     if ((pptr() >= epptr()) && flush_output(T_MORE) && !doallocate()) return EOF;
  124.  
  125. // store char
  126.     *pptr() = c; pbump(1);
  127.     
  128.     return c;
  129. }
  130.  
  131. // ---------------------------------------------------------------------------
  132. //     doallocate()
  133. // ---------------------------------------------------------------------------
  134. //    return true if alocation
  135.  
  136. Boolean TNetworkBuf::doallocate()
  137. {
  138.     if(!pbase() && (fBuffer = (char*) ::OTAllocMem(fChunkSize) )) {
  139.         setp(fBuffer, fBuffer + fChunkSize );
  140.         return true;
  141.         }
  142.     return  false;
  143. }
  144.  
  145. // ---------------------------------------------------------------------------
  146. //     xputn(char*, len)
  147. // ---------------------------------------------------------------------------
  148. //    stuff chars into output buffer
  149.  
  150. int TNetworkBuf::xsputn( const char *s, int len)
  151. {
  152.     const unsigned char *p = (const unsigned char*) s;
  153.     
  154.     if(len <= 0) return 0;
  155.     
  156.     for(int i = 0; i<len; i++, p++)
  157.         if( sputc(*p) == EOF ) return i;
  158.         
  159.     return len;
  160. }
  161.  
  162.  
  163. // ---------------------------------------------------------------------------
  164. //     flush_output()
  165. // ---------------------------------------------------------------------------
  166. //    flush output buffer
  167.  
  168. int TNetworkBuf::flush_output(OTFlags flags)
  169. {
  170.     int len = fBuffer ? pptr() - pbase(): 0;
  171.  
  172. // can only output when open.
  173.     if (! is_open()) return EOF;
  174.  
  175. // out any data
  176.     if (len || (fInfo->flags & T_SENDZERO) ) {
  177.         ::OTSnd(fEndPoint, fBuffer, len , flags);
  178.         fBuffer = nil;
  179.         setp(0,0);
  180.         }
  181.     
  182.     return len;
  183. }
  184.  
  185.  
  186.  
  187. // ---------------------------------------------------------------------------
  188. //     underflow()
  189. // ---------------------------------------------------------------------------
  190. //    flush output buffer
  191.  
  192. int TNetworkBuf::underflow()
  193. {
  194.     return EOF;
  195. }
  196.  
  197.  
  198. // ---------------------------------------------------------------------------
  199. //     uflow()
  200. // ---------------------------------------------------------------------------
  201. //     
  202.  
  203. int TNetworkBuf::uflow()
  204. {
  205.     return EOF;
  206. }
  207.  
  208.  
  209. // ---------------------------------------------------------------------------
  210. //     Release(buf);
  211. // ---------------------------------------------------------------------------
  212. //    Release buffer chain
  213.  
  214. void TNetworkBuf::Release(void* buf)
  215. {
  216.     ::OTFreeMem( buf );
  217. }
  218.  
  219.  
  220.  
  221. // ---------------------------------------------------------------------------
  222. //     buf = ReserveBuffer(req, resp );
  223. // ---------------------------------------------------------------------------
  224. //    
  225. ////// WRITE ME
  226.  
  227. char* TNetworkBuf::ReserveBuffer(size_t  reqCount, size_t  *actCount )
  228. {
  229.  
  230.     *actCount  =  (reqCount > BUFFSIZE) ? BUFFSIZE : reqCount ; 
  231.     return ( (char*) Buffer );
  232. }
  233.  
  234.  
  235.  
  236. // ---------------------------------------------------------------------------
  237. //     EnqueueBuffer(buf, size );
  238. // ---------------------------------------------------------------------------
  239. //    
  240. ////// WRITE ME
  241.  
  242. void  TNetworkBuf::EnqueueBuffer(char* buf, size_t count )
  243. {
  244.     
  245.     if(buf) if( count);
  246. }
  247.  
  248.  
  249.  
  250.  
  251.